<template>
  <el-dialog
    :visible.sync="show"
    :title="{ add: '新增角色', edit: '编辑角色' }[mode]"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRolePost, sysRolePut } from '@/api/setting'
export default {
  data() {
    return {
      mode: 'add',
      show: false,
      form: {
        name: '', //	string	必须		角色名称
        description: '' //	string	必须		角色描述
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 2,
            max: 20,
            message: '请输入2-20个字符',
            trigger: 'change'
          }
        ], //	string	必须		角色名称
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	必须		角色名称
        description: '' //	string	必须		角色描述
      }
    },
    async submit() {
      await this.$refs.form.validate()
      if (this.mode === 'add') {
        await sysRolePost(this.form)
        this.$message.success('新增角色成功')
      } else if (this.mode === 'edit') {
        await sysRolePut(this.form)
        this.$message.success('修改角色成功')
      }
      this.show = false
      this.$emit('getData')
    }
  }
}
</script>
<style></style>
